﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting;

namespace BHair.Base
{
    public partial class frmmembernew : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmmembernew()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        public void jzkl()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "select * from card";
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            sda.Fill(ds,"card");
            comboBox1.DataSource = ds.Tables["card"];
            comboBox1.DisplayMember = "cname";
            conn.Close();
        }
        public DataSet jzmemberall()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "select * from member";
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            sda.Fill(ds,"member");
            // sda.Fill(dt);
            //gridControl1.DataSource = dt;
            gridControl1.DataSource = ds.Tables["member"];
            conn.Close();
            return ds;
        }
        private void frmmembernew_Load(object sender, EventArgs e)
        {
            jzkl();
            jzmemberall();
           // gridView1.OptionsBehavior.EditorShowMode=select;


        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
            xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\会员管理.repx");
            xr.DataSource = jzmemberall();
            xr.ShowPreview();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {

            PrintableComponentLink link = new PrintableComponentLink(new PrintingSystem());
            link.Component = this.gridControl1;
            link.Landscape = true;
            link.PaperKind = System.Drawing.Printing.PaperKind.A3;
            link.CreateMarginalHeaderArea += new CreateAreaEventHandler(Link_CreateMarginalHeaderArea);
            link.CreateDocument();
            link.ShowPreview();
        }
        private void Link_CreateMarginalHeaderArea(object sender, CreateAreaEventArgs e)
        {
            string title = string.Format("会员信息");
            PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.None, title, Color.DarkBlue,
               new RectangleF(0, 0, 100, 21), BorderSide.None);

            brick.LineAlignment = BrickAlignment.Center;
            brick.Alignment = BrickAlignment.Center;
            brick.AutoWidth = true;
            brick.Font = new System.Drawing.Font("宋体", 11f, FontStyle.Bold);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text != string.Empty)
            {
                selectkfl();
            }
            else
            {
                jzmemberall();
            }
            
        }
        public DataSet selectkfl()
        {
            //根据分类名称查询分类的id
            SqlConnection conn1 = new SqlConnection(constr);
            conn1.Open();
            string sqlstr1 =string.Format( "select cid from card where cname='{0}'",comboBox1.Text);
            SqlCommand comm = new SqlCommand(sqlstr1,conn1);
            SqlDataReader sdr = comm.ExecuteReader();
            int fl = 0;
            while (sdr.Read())
            {
                fl =Int32.Parse( sdr["cid"].ToString());
            }
            conn1.Close();


            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("select * from member where mcid={0}",fl);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            sda.Fill(ds, "member");
            // sda.Fill(dt);
            //gridControl1.DataSource = dt;
            gridControl1.DataSource = ds.Tables["member"];
            conn.Close();
            return ds;
        }

        private void gridControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ShowMember();
        }
        private void ShowMember()
        {
            
                string strMemberId = gridView1.GetFocusedRowCellValue("mid").ToString();
            frmMember objfrmMember = new frmMember(strMemberId);
                if (objfrmMember.ShowDialog() == DialogResult.OK)
                {
                    //this.LoadMemberList();
                }
         }
        private void gridView1_CustomColumnDisplayText(object sender,
            DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "msex")
            {
                switch (e.Value.ToString().Trim())
                {
                    case "1":
                        e.DisplayText = "男";
                        break;
                    case "0":
                        e.DisplayText = "女";
                        break;
                    default:
                        e.DisplayText = "";
                        break;
                }
            }
        }

    }
    }

